<template>


    <el-button type="primary"  @click="handleAdd">
       新增字典
      </el-button>
    <el-table :data="tableData" style="width: 100%">
        <el-table-column label="字典类型" prop="dictType" />
        <el-table-column label="字典键值" prop="dictValue" />
        <el-table-column label="字典标签" prop="dictLabel" />
        <el-table-column label="字典排序" prop="dictSort" />
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column align="right">
          <template #header>
        <el-input v-model="queryForm.query"  @input="initUserList" placeholder="请输入内容"></el-input>
          </template>
          <template #default="scope">
            <el-button size="small"  type="primary" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="deleteDic( scope.row.dictCode)" >
             删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="queryForm.pageNum"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :size="size"
    
          background layout="prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
    
        <el-dialog v-model="dialogFormVisible" title="新增字典" width="500">
          <el-form :model="dictFrom" :rules="rules" ref="formRef" label-width="auto" style="max-width: 600px">
   
        <el-form-item prop="dictType" label="字典类型">
          <el-input v-model="dictFrom.dictType" placeholder="请输入字典类型"></el-input>
        </el-form-item>
        <el-form-item prop="dictValue" label="字典键值">
          <el-input   v-model="dictFrom.dictValue" placeholder="请输入字典键值"></el-input>
        </el-form-item>
        <el-form-item label="字典标签" prop="dictLabel" >
            <el-input v-model="dictFrom.dictLabel"  placeholder="请输入字典标签" />
          </el-form-item>
          <el-form-item label="字典排序" prop="dictSort" >
            <div style="margin: 20px 0" />
            <el-input v-model="dictFrom.dictSort"  placeholder="请输入字典排序"/>
          </el-form-item>
       
      </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="resetForm">取消</el-button>
            <el-button type="primary" @click=" submitForm()">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    
    </template>
    
    <script  setup>
         import {ref,reactive} from 'vue';
         import request from '@/util/request';
         import { ElMessage,ElMessageBox } from 'element-plus'
        
         const dialogFormVisible = ref(false)
         const tableData=ref([]);
         const total=ref(0);
         const queryForm=ref({
            query:"",
            pageNum:1,
            pageSize:10
         })
         const initUserList=async ()=>{
           const res= await request.post("/dict/list",queryForm.value)
           tableData.value=res.data.data.dataList;
           total.value=res.data.data.total;
         }
         initUserList();
         const handleSizeChange=(pageSize)=>{
            queryForm.value.pageNum=1;
            queryForm.value.pageSize=pageSize
            initUserList();
         }
         const handleCurrentChange=(pageNum)=>{
            queryForm.value.pageNum=pageNum;
             
            initUserList();
         }

         const dictFrom = reactive({
          dictCode:'',
          dictSort:0,
          dictLabel:'',
          dictValue: '',
          dictType: ''
    })
    
    const formRef = ref(null);
    const rules = {
      dictType: [
        { required: true, message: '请输入字典类型', trigger: 'blur' }
      ],
      dictValue: [
        { required: true, message: '请输入字典值', trigger: 'blur' }
      ],
      dictLabel: [
                { required: true, message: '请输入字典名称', trigger: 'blur' }
              
        ]
    };
    const deleteDic=(id)=>{
  ElMessageBox.confirm(
          '确认删除',
          'Warning',
          {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
          }
      ).then(() => {
          request.get("dict/"+id).then(res=>{
            if(res.data.code==200){
              initUserList()
              ElMessage.success("删除成功")
            }else{
              ElMessage.error(res.data.msg);
            }
          })
    })}
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          request.post("dict/saveOrUpdate",dictFrom).then(res=>{
           if(res.data.code==200){
            initUserList();
            resetForm();
            ElMessage.success("新增成功")
           }else{
            ElMessage.error(res.data.msg);
           }
          })
        } else {
          return false;
        }
      });
    };
    const handleEdit=(index,row)=>{
     let obj=  Object.assign({}, row)
     dictFrom.dictCode=obj.dictCode;
     dictFrom.dictSort=obj.dictSort;
     dictFrom.dictLabel=obj.dictLabel;
     dictFrom.dictValue=obj.dictValue;
     dictFrom.dictType=obj.dictType;
    
    dialogFormVisible.value = true
    }
    
    const handleAdd=()=>{
      dialogFormVisible.value = true
    }
    
    const resetForm = () => {
      dictFrom.dictCode='';
     dictFrom.dictSort='';
     dictFrom.dictLabel='';
     dictFrom.dictValue='';
     dictFrom.dictType='';
          dialogFormVisible.value = false
        }
    </script>
    
    <style lang="scss" scoped>
    
    </style>
    